﻿namespace com.oakwoodsys.ConsultantCareDao
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using com.oakwoodsys.SharePoint;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Linq;

    using Context = ConsultantCareDataContext;

    using ReferenceObject = ConsultantCareActivity;

    public class ConsultantCareActivityDao : BaseDao
    {
        #region Fields

        public const string ListName = "activities";

        #endregion Fields

        #region Constructors

        public ConsultantCareActivityDao( )
            : base(ListName)
        {
            CurrentWebUrl = SPContext.Current.Web.Url;
        }

        public ConsultantCareActivityDao( string currentWebUrl )
            : base(ListName)
        {
            CurrentWebUrl = currentWebUrl;
        }

        #endregion Constructors

        #region Methods

        public ReferenceObject Get( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                ReferenceObject v = ( from o in l
                                      where o.Id.HasValue && o.Id.Value == id
                                      select o ).SingleOrDefault( );
                return v;
            }
        }

        public ReferenceObject Get( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                ReferenceObject v = ( from o in l
                                      where o.UniqueId == id
                                      select o ).SingleOrDefault( );
                return v;
            }
        }

        public List<ReferenceObject> GetAll( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                List<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle ).ToList( );
                return l;
            }
        }

        public List<ReferenceObject> GetAllConsultantAssociated( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                List<ReferenceObject> v = ( from o in l
                                            where
                                            ( o.Associated == Associated.Consultant && o.Active.HasValue && o.Active.Value ) ||
                                            ( o.Associated == Associated.Both && o.Active.HasValue && o.Active.Value )
                                            select o ).DefaultIfEmpty( ).ToList( );
                if ( v.Count == 1 && v[ 0 ] == null )
                    v.Clear( );
                return v;
            }
        }

        public List<ReferenceObject> GetAllPermanentAssociated( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                List<ReferenceObject> v = ( from o in l
                                            where
                                            ( o.Associated == Associated.Permanent && o.Active.HasValue && o.Active.Value ) ||
                                            ( o.Associated == Associated.Both && o.Active.HasValue && o.Active.Value )
                                            select o ).DefaultIfEmpty( ).ToList( );
                if ( v.Count == 1 && v[ 0 ] == null )
                    v.Clear( );
                return v;
            }
        }

        public ReferenceObject Update( ReferenceObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                ReferenceObject v = ( from o in l
                                      where o.UniqueId == value.UniqueId
                                      select o ).SingleOrDefault( );

                if ( v != null )
                {
                    v.Active = value.Active;
                    v.Associated = value.Associated;
                    v.IsReminder = value.IsReminder;
                    v.Message = value.Message;
                    v.NDays = value.NDays;
                    v.Recurring = value.Recurring;
                    v.Title = value.Title;
                    // commit changes
                    c.SubmitChanges( );
                    return v;

                }
                else
                    return null;
            }
        }

        #endregion Methods
    }
}